'use strict';

angular.module('homepageApp')
    .directive("scrollingTo", ["$window", function($window){
        return {
            restrict : "AC",
            compile : function(){

                var document = $window.document;

                function scrollInto(idOrName) {//find element with the give id of name and scroll to the first element it finds
                    //console.log("input value: " + idOrName)
                    if(!idOrName) { //id is not provided. so scroll to top of the page.
                        $window.scrollTo(0, 0);
                    }
                    else {
                        var el = document.getElementById(idOrName); //check if an element can be found with id attribute
                        //console.log("el value by id:" + el);
                        if (!el) {
                            el = document.getElementsByName(idOrName);
                            //console.log("el value by name:" + el);
                            if (el && el.length > 0)
                                el = el[0];
                            else
                                el = null;
                        }

                        if (el) //if an element is found, scroll to the element
                            el.scrollIntoView();
                        //otherwise, ignore
                    }
                }
                return function(scope, element, attr) {
                    element.bind("click", function(event){
                        scrollInto(attr.scrollingTo);
                    });
                };
            }
        };
    }]);
